package com.hou.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hou.eduservice.entity.EduCourse;
import com.hou.eduservice.entity.EduCourseDescription;
import com.hou.eduservice.entity.frontvo.CourseFrontVo;
import com.hou.eduservice.entity.vo.CourseInfoVo;
import com.hou.eduservice.entity.vo.CoursePublicVo;
import com.hou.eduservice.entity.frontvo.CourseWebVo;
import com.hou.eduservice.mapper.EduCourseMapper;
import com.hou.eduservice.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-10-17
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService descriptionService;

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private EduChapterService eduChapterService;

    @Override
    public String saveInfo(CourseInfoVo courseInfoVo) throws Exception {

        // 获取除简介外的课程信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);

        // 添加信息
        int insert = baseMapper.insert(eduCourse);

        // 判断添加是否成功
        if (insert <= 0) {
            throw new Exception("添加失败");
        }

        // 获取简介信息
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoVo.getDescription());

        // 获取课程id并加入简介类中，使两张表产生联系
        eduCourseDescription.setId(eduCourse.getId());

        // 添加简介信息
        descriptionService.save(eduCourseDescription);

        // 返回id
        return eduCourse.getId();
    }

    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        EduCourse eduCourse = baseMapper.selectById(courseId);

        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse, courseInfoVo);

        EduCourseDescription description = descriptionService.getById(courseId);
        courseInfoVo.setDescription(description.getDescription());

        return courseInfoVo;
    }

    @Override
    public void updateCourse(CourseInfoVo courseInfoVo) {

        // 修改course信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);

        baseMapper.updateById(eduCourse);

        // 修改courseDescription信息
        EduCourseDescription description = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoVo, description);

        descriptionService.updateById(description);
    }

    // 获取课程全部信息
    @Override
    public CoursePublicVo getAll(String id) {
        CoursePublicVo courseVo = baseMapper.getCourseVo(id);
        return courseVo;
    }


    // 删除课程全部信息
    @Override
    public void deleteAllInfo(String id) throws Exception {

        // 删除描述
        descriptionService.removeById(id);

        // 删除小节
        eduVideoService.deleteVideoById(id);

        // 删除章节
        eduChapterService.deleteChapterById(id);

        // 删除章节本身
        int flag = baseMapper.deleteById(id);

        if (flag == 0) {
            throw new Exception("删除失败！");
        }

    }

    // 获取前八条热门课程
    @Override
    public List<EduCourse> getHotCourse() {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        wrapper.last("limit 4");

        List<EduCourse> eduCourses = baseMapper.selectList(wrapper);

        return eduCourses;
    }

    // 课程条件查询带分页
    @Override
    public Map<String, Object> getCourseLimit(Page<EduCourse> pageLimit, CourseFrontVo course) {

        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(course.getSubjectParentId())) {
            queryWrapper.eq("subject_parent_id", course.getSubjectParentId());
        }

        if (!StringUtils.isEmpty(course.getSubjectId())) {
            queryWrapper.eq("subject_id", course.getSubjectId());
        }

        if (!StringUtils.isEmpty(course.getBuyCountSort())) {
            queryWrapper.orderByDesc("buy_count");
        }

        if (!StringUtils.isEmpty(course.getGmtCreateSort())) {
            queryWrapper.orderByDesc("gmt_create");
        }

        if (!StringUtils.isEmpty(course.getPriceSort())) {
            queryWrapper.orderByDesc("price");
        }

        baseMapper.selectPage(pageLimit,queryWrapper);


        // 每页数据集合
        List<EduCourse> records = pageLimit.getRecords();
        // 当前页
        long current = pageLimit.getCurrent();
        // 总页数
        long pages = pageLimit.getPages();
        // 每页记录数
        long size = pageLimit.getSize();
        // 总记录数
        long total = pageLimit.getTotal();

        // 是否有下一页
        boolean hasNext = pageLimit.hasNext();
        // 是否有上一页
        boolean hasPrevious = pageLimit.hasPrevious();

        HashMap<String, Object> map = new HashMap<>();

        map.put("records",records);
        map.put("current",current);
        map.put("pages",pages);
        map.put("size",size);
        map.put("total",total);
        map.put("hasNext",hasNext);
        map.put("hasPrevious",hasPrevious);

        return map;

    }

    // sql获取课程信息
    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }

}
